from concurrent.futures import ThreadPoolExecutor
import requests
import uuid
import json
import sys
import os
import time

sys.path.append(
    os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(__file__))))
)

from proxy.tunnel_proxy_service_qg import TunnelProxyService_QG
from common.logger import logger
from common.recognize import recognize_number

# 生成guid

proxy = TunnelProxyService_QG().take().build()

import threading


def get_verify_token(info):
    code = recognize_number(info["verifyUrl"]).strip()

    data = {
        "key": info["verifyKey"],
        "userId": "c86e57495f504b94a5da9c047ac393fe",
        "applictionType": "APP",
        "captchaCode": code,
        "signType": "MD5",
        "verifyType": "ORDER",
    }

    headers = {
        "Host": "sytgate.jslife.com.cn",
        "Content-Type": "application/json;charset=utf-8",
    }

    url = "https://sytgate.jslife.com.cn/core-gateway/user/order/verify/image"

    rspJson = requests.post(
        url, headers=headers, proxies=proxy, data=json.dumps(data)
    ).json()

    print(rspJson)

    return rspJson["obj"]["token"]


def test():
    token = ""

    start_time = time.time()

    success_count = 0
    verify_count = 0

    while True:
        try:
            userId = uuid.uuid4().hex

            userId = "c86e57495f504b94a5da9c047ac393fe"

            data = {"carNo": "鄂-A11111", "userId": userId, "verifyToken": token}

            data = json.dumps(data)

            headers = {
                "Host": "sytgate.jslife.com.cn",
                "Accept": "*/*",
                "Content-Type": "application/json;charset=UTF-8",
                "User-Agent": "JTC/6.0.6 (iPhone; iOS 16.3.1; Scale/3.00)",
            }

            url = "https://sytgate.jslife.com.cn/core-gateway/order/carno/pay/appindex"

            rsp = requests.post(
                url,
                data=data,
                proxies=proxy,
                headers=headers,
            )

            rspJson = rsp.json()

            if rspJson["resultCode"] == "120003":
                logger.error(rspJson["resultCode"])
                break

            if rspJson["resultCode"] == "123012":
                logger.warning(f"开始校验")
                token = get_verify_token(rspJson["obj"]["verifyInfo"])
                logger.warning(f"校验成功:{token}")
                verify_count += 1
                continue

            if rspJson["resultCode"] == "0":
                logger.success(rspJson["resultCode"])
                success_count += 1
            else:
                logger.error(rspJson["resultCode"])

        except Exception as e:
            # logger.error(e)
            pass

    logger.success(
        f"耗时:{time.time() - start_time}, 成功:{success_count}, 校验:{verify_count}"
    )


if __name__ == "__main__":
    thread_count = 1

    thread_pool = ThreadPoolExecutor(max_workers=thread_count)

    futures = []
    for i in range(thread_count):
        futures.append(thread_pool.submit(test))

    thread_pool.shutdown(wait=True)
